﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Script.Services;
using System.Text;

public partial class UserControls_DictSelecter : BaseUserConrolForAjax
{
    private string jsUrl = "../js/select.js";
    private string cssUrl = "../css/select.css";
    private string strParentId="2";
    private string dataSouceServiceUrl = "../WebService/DataForAjax.asmx";
    private string dictText = "";

    /// <summary>
    /// 设置或者获取控件格式化后的字典值
    /// </summary>
    public string DictText
    {
        get { return dictText; }
        set { dictText = value; }
    }

    /// <summary>
    /// 用于数据更新的webservice地址
    /// </summary>
    public string DataSouceServiceUrl
    {
        get { return dataSouceServiceUrl; }
        set { dataSouceServiceUrl = value; }
    }

    /// <summary>
    /// 代码集合的父id;
    /// </summary>
    public string StrParentId
    {
        get { return strParentId; }
        set { strParentId = value; }
    }


    /// <summary>
    /// selectcss地址
    /// </summary>
    public string CssUrl
    {
        get { return cssUrl; }
        set { cssUrl = value; }
    }

    /// <summary>
    /// selectJs地址
    /// </summary>
    public string JsUrl
    {
        get { return jsUrl; }
        set { jsUrl = value; }
    }

    private string divId;


    /// <summary>
    /// 控件值
    /// </summary>
    public string Text
    {
        get { return this.txtSelected.Text; }
        set { this.txtSelected.Text = value; }
    }

    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!IsPostBack)
        {
            this.GetDivId();
            this.ltrDiv.Text = this.GetDivInnerHtml();
            this.ShowBtnSelect();
        }
        this.ShowOutJsAndCss();
    }

    private void GetDivId()
    {
        this.divId = Guid.NewGuid().ToString();
    }

    private string GetDivInnerHtml()
    {
        return "<div id=\"" + this.divId + "\" Name=\"DialogDiv\" style=\"position:absolute;\"></div>";
    }

    private void ShowBtnSelect()
    {
        //this.ltrBtnSelect.Text = "<input type=\"button\" value=\"请选择\" id=\"btn_select\" onClick=\"select(\"" + this.txtSelected.ClientID + "\",\"" + this.supplyUrl + "\",\"" + this.divId + "\")\">";
        this.ltrBtnSelect.Text = "<input type=\"button\" value=\"请选择\" id=\"btn_select\" onclick=\"javascript:selectDict('" + this.txtSelected.ClientID + "','" + this.strParentId + "','" + this.divId + "');\">";
    }

    /// <summary>
    /// 输出不重复的js和css
    /// </summary>
    /// <returns></returns>
    private void ShowOutJsAndCss()
    {
        if (this.Page.Header.FindControl("ltrSelectJs") == null)
        {
            Literal lit = new Literal();
            lit.ID = "ltrSelectJs";
            lit.Text = "\r\n<script language=\"javascript\" src=\"" + this.jsUrl + "\"></script>\r\n";
            //lit.Text = "\r\n<script language=\"javascript\" src=\"" + HttpContext.Current.Server.UrlPathEncode(HttpContext.Current.Request.ApplicationPath.ToString()) + this.jsUrl + "\"></script>\r\n";
            this.Page.Header.Controls.Add(lit);
        }
        if (this.Page.Header.FindControl("ltrSelectCss") == null)
        {
            Literal lit2 = new Literal();
            lit2.ID = "ltrSelectCss";
            lit2.Text = "\r\n<link href=\"" + this.cssUrl + "\" rel=\"stylesheet\" type=\"text/css\">\r\n";
            this.Page.Header.Controls.Add(lit2);
        }

    }

    protected override void IsRegistScriptManager()
    {
        if (this.Page.Header.FindControl("SmIsExist") == null)
        {
            ScriptManager sm = new ScriptManager();
            ServiceReference sr = new ServiceReference();
            sr.Path = this.dataSouceServiceUrl;
            sm.Services.Add(sr);
            sm.ID = "ajaxScriptManager";
            this.Controls.Add(sm);

            //添加存在标识符
            Literal lr = new Literal();
            lr.ID = "SmIsExist";
            this.Page.Header.Controls.Add(lr);
        }
    }

    #region 方法
    /// <summary>
    /// 获取“,”分割的代码集合
    /// </summary>
    /// <returns></returns>
    public string GetFormatDcit()
    {
        //3:企业动态,4:合作案例,
        StringBuilder sbFormatDict=new StringBuilder();
        if (Text != "")
        {
            string[] strArrFormat = Text.Split(':');
            for (int i = 0; i < strArrFormat.Length-1; i++)
            { 
                sbFormatDict.Append(strArrFormat[i].Substring(strArrFormat[i].Length-1,1));
                sbFormatDict.Append(",");
            }
            return sbFormatDict.ToString();
        }
        else
        {
            return "";
        }
    }

    /// <summary>
    /// 获取int型代码集合
    /// </summary>
    /// <returns></returns>
    public int[] GetIntFormatDcit()
    {
        StringBuilder sbFormatDict = new StringBuilder();
        if (Text != "")
        {
            string[] strArrFormat = Text.Split(':');
            int[] items_ = new int[strArrFormat.Length - 1];
            for (int i = 0; i < strArrFormat.Length - 1; i++)
            {
                items_[i] = Convert.ToInt32(strArrFormat[i].Substring(strArrFormat[i].Length - 1, 1));
            }
            return items_;
        }
        else
        {
            return null;
        }
    }
    #endregion
}